//
//  DestinationViewController.m
//  Tour
//
//  Created by 张张烨 on 16/3/7.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "DestinationViewController.h"
#import "PrefixHeader.pch"

@interface DestinationViewController ()<UITableViewDataSource, UITableViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate>
{
    // HUD (Head-Up Display, 意思是抬头显示的意思)
    MBProgressHUD *HUD;
}
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *collectionViewArr; /**<collectionView数组*/

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;/**<tableView数组*/
@property(nonatomic, retain)UIView *btnView; //tableView上新建的View 各大洲
@property(nonatomic, retain)UIButton *afBtn;/**<非洲*/
@property(nonatomic, retain)UIButton *eBtn;/**<欧洲*/
@property(nonatomic, retain)UIButton *aBtn;/**<亚洲*/
@property(nonatomic, retain)UIButton *saBtn;/**<南美*/
@property(nonatomic, retain)UIButton *naBtn;/**<北美*/
@property(nonatomic, retain)UIButton *oceBtn;/**<大洋*/
@property(nonatomic, retain)UIButton *antBtn;/**<南极*/

@property(nonatomic, assign)NSInteger i;


@property (nonatomic, retain)NSArray *nameId;/**<名字id数组*/


@end

@implementation DestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//标题
    self.navigationItem.title = @"目的地";
    
    // 设置初值
    [self dataHandler:@"10"];
    
    //创建tableView
    [self creatTableView];
    
    //七大洲
    [self btnSet];
    
    //创建collectionView
    [self creatCollectionView];
    
    _i = 100;
    
    // 调用菊花类
    [self showTextDialog];
    
   
}
#pragma mark - 创建tableview
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    //注册重用池
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //添加父视图
    [self.view addSubview:_tableView];
    
 
}
#pragma mark - 小菊花实现
- (void)showTextDialog
{
    //初始化进度框，置于当前的View当中
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    
    //如果设置此属性则当前的view置于后台
    HUD.dimBackground = YES;
    
    //设置对话框文字
    HUD.labelText = @"请稍等";
    
    //显示对话框
    [HUD showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(5);
    } completionBlock:^{
        //操作执行完后取消对话框
        [HUD removeFromSuperview];
        
        HUD = nil;
    }];
}

#pragma mark - 创建collectionView
- (void)creatCollectionView
{
    //collectionView标题
    UILabel *collectionViewTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, SCREEN_WIDTH, 30)];
    
   
    collectionViewTitle.text = @"热门目的地";
    collectionViewTitle.font = [UIFont systemFontOfSize:15];
    collectionViewTitle.backgroundColor = [UIColor whiteColor];
    [collectionViewTitle NightWithType:UIViewColorTypeNormal];
    [self.btnView addSubview:collectionViewTitle];
    //tableView标题
    UILabel *tableViewTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT + 440, 100, 30)];
    
    
    tableViewTitle.text = @"其他目的地";
    tableViewTitle.backgroundColor = [UIColor whiteColor];
    tableViewTitle.font = [UIFont systemFontOfSize:15];
    [tableViewTitle NightWithType:UIViewColorTypeNormal];
    [self.btnView addSubview:tableViewTitle];
    
    
    
    ///创建collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置滚动方向为垂直
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置cell的大小
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 30 ) / 2, (SCREEN_HEIGHT  - 40) / 3);
layout.minimumInteritemSpacing = 10;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 230, SCREEN_WIDTH, SCREEN_HEIGHT + 200) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    //设置代理人
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    //弹动效果
    _collectionView.bounces = NO;
    //隐藏滚动条
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    [_collectionView NightWithType:UIViewColorTypeNormal];
    
    //注册重用池
    [_collectionView registerClass:[DesCell class] forCellWithReuseIdentifier:@"image"];
    //添加在btnView上
    [self.btnView addSubview:_collectionView];
    
}

#pragma mark - 数据处理
- (void)dataHandler:(NSString *)cid
{
       [AFNModel GETWithHandler:@"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88241944084977&lon=121.5398896720327&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&v=1" completion:^(id result) {
        
        
        NSMutableArray *arr = result[@"data"];
        
        
        //遍历数组
        for (NSDictionary *dic in arr) {
            if ([@"10" isEqualToString:[dic[@"id"] stringValue]]) {
                
                //tableView数组获取
                self.dataArr = [CollectionViewModel modelHanderWithArray:dic[@"country"]];
               
                
                //collectionView 数组获取
                self.collectionViewArr = [CollectionViewModel modelHanderWithArray:dic[@"hot_country"]];
                
            }
            
            else if ([cid isEqualToString:[dic[@"id"] stringValue]])
            
            {
                //tableView数组获取
                self.dataArr = [CollectionViewModel modelHanderWithArray:dic[@"country"]];
                //collectionView 数组获取
                self.collectionViewArr = [CollectionViewModel modelHanderWithArray:dic[@"hot_country"]];
                
            }
                
        }
      
       //刷新
        [_collectionView reloadData
         ];
        [_tableView reloadData];
        
        
    }];
}

#pragma mark - 七大洲按钮创建
- (void)btnSet
{
    self.btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT + 480 )];
    _btnView.backgroundColor = [UIColor whiteColor];
    UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"qidazhou.jpg"]];
    backImage.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
//    [self.btnView NightWithType:UIViewColorTypeNormal];
    [self.btnView addSubview:backImage];
    //将btnView添加到tableView的tableHeaderView上
    
    _tableView.tableHeaderView = _btnView;
    NSArray *nameArr = @[@"亚洲",@"欧洲",@"非洲",@"南美洲",@"北美洲",@"大洋洲",@"南极洲"];
    self.nameId = @[@"10",@"12", @"76", @"235", @"234", @"239", @"759"];
    for (int i = 0; i < nameArr.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSString *title = [nameArr objectAtIndex:i];
        
        [button setTag:i + 100];
        
        //判断第一个按钮的状态
        if(i == 0){
            button.selected = YES;
            
        }
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        
        button.layer.cornerRadius = 5;
        button.font = [UIFont systemFontOfSize:16];
        [button setTitle:title forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
        
        switch (i) {
            case 0:
               button.frame =CGRectMake(255, 35, 50, 20);
                break;
            case 1:
                button.frame =CGRectMake(185, 40, 50, 20);
                break;
            case 2:
                
                 button.frame =CGRectMake(175, 110, 50, 20);
                break;
            case 3:
                button.frame =CGRectMake(100, 140, 50, 20);
                break;
            case 4:
                button.frame =CGRectMake(65, 30, 50, 20);
                break;
            case 5:
                button.frame =CGRectMake(290, 130, 50, 20);
                break;
            case 6:
                button.frame =CGRectMake(175, 180, 50, 20);
                break;
            default:
                break;
        }
        
        [self.btnView addSubview:button];
    };
}



#pragma mark - 七大洲按钮实现点击方法
- (void)buttonSelected:(UIButton *)btn
{
    
    NSString *dataId = [ self.nameId objectAtIndex:btn.tag - 100];
    [self dataHandler:dataId];
    // 菊花类
    [self showTextDialog];
    if (btn.tag != _i) {
        
        UIButton *lastButton = (UIButton *)[self.view viewWithTag:_i];
        
        lastButton.selected = NO;
        _i = btn.tag;
    }
    if (!btn.selected) {
        btn.selected = YES;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    NSLog(@"非洲");
}

#pragma mark - tableview协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    CollectionViewModel *s = _dataArr[indexPath.row];
    
    cell.textLabel.text =[NSString stringWithFormat:@"%@ %@",s.cnname,s.enname];
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    [cell.textLabel NightWithType:UIViewColorTypeNormal];
    [cell NightWithType:UIViewColorTypeNormal];
    return cell;
    
    
}

#pragma mark - 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.hidesBottomBarWhenPushed = YES;
    //取消tableview 选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QpHotCountryViewController *hotcountry = [[QpHotCountryViewController alloc]init];
    CollectionViewModel *s = _dataArr[indexPath.row];
    // 传国家id
    hotcountry.idStr = [s.idstr stringValue];

    [self.navigationController pushViewController:hotcountry animated:YES];
    // 跳转隐藏tabbar
    self.hidesBottomBarWhenPushed = NO;

    
}

#pragma mark - collectionview 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
   
    return _collectionViewArr.count;
    
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"image" forIndexPath:indexPath];

    CollectionViewModel *c = _collectionViewArr[indexPath.item];
    // 网络获取 图片
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:c.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    // 中文名字
    cell.cnname.text = c.cnname;
    // English Name
    cell.enname.text = c.enname;
    // 个数
    cell.count.text = [c.count stringValue];
    // 城市
    cell.label.text = c.label;
//    [cell.cnname NightWithType:UIViewColorTypeNormal];
//    [cell.enname NightWithType:UIViewColorTypeNormal];
//    [cell.count NightWithType:UIViewColorTypeNormal];
//    [cell.label NightWithType:UIViewColorTypeNormal];
    [cell NightWithType:UIViewColorTypeNormal];
    
    return cell;
    
}
#pragma mark - 点击item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    self.hidesBottomBarWhenPushed = YES;
    QpHotCountryViewController *hotcountry = [[QpHotCountryViewController alloc]init];
    CollectionViewModel *h = _collectionViewArr[indexPath.item];
    // 传 热门国家id
    if ([[h.idstr stringValue] isEqualToString:@"10723"]) {
        
    }else{
        hotcountry.idStr = [h.idstr stringValue];
        hotcountry.lable = h.label;
        [self.navigationController pushViewController:hotcountry animated:YES];
        // 跳转隐藏tabbar
        self.hidesBottomBarWhenPushed = NO;
           }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
